package JavaCollection;


import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 4.3.4 自定义类的常用操作
 */

public class HashSetDemo4 {
    static class ImoocStudent {
        private String nickname;

        private String position;

        public ImoocStudent() {
        }

        public ImoocStudent(String nickname, String position) {
            this.setNickname(nickname);
            this.setPosition(position);
        }

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getPosition() {
            return position;
        }

        public void setPosition(String position) {
            this.position = position;
        }

        // 不是很懂这个重写的方法s
        @Override
        public String toString() {
            return "ImoocStudent{" +
                    "nickname='" + nickname + '\'' +
                    ", position='" + position + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        // 实例化一个新的空集
        Set<HashSetDemo4.ImoocStudent> hashSet = new HashSet<>();
        // 实例化3个慕课网学生对象
        ImoocStudent imoocStudent1 = new ImoocStudent("Colorful","服务端工程师");
        ImoocStudent imoocStudent2 = new ImoocStudent("Lillian","客户端工程师");
        ImoocStudent imoocStudent3 = new ImoocStudent("小慕","架构师");
        // 新增元素
        hashSet.add(imoocStudent1);
        hashSet.add(imoocStudent2);
        hashSet.add(imoocStudent3);
        // 使用 Iterator 遍历 hashSet
        Iterator<ImoocStudent> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        // 查找并删除
        if (hashSet.contains(imoocStudent1)) {
            hashSet.remove(imoocStudent1);
        }
        System.out.println("删除nickname为Colorful的对象后, 集合元素为：");
        System.out.println(hashSet);
    }
}
